/**
 * 
 */
package com.gr.staffpm.pages.schedule;

import java.util.List;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.google.utils.GCalUtils;
import com.gr.staffpm.pages.schedule.model.Calendar;
import com.gr.staffpm.pages.schedule.model.Event;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.service.TaskService;


/**
 * @author Graham Rhodes 26 Apr 2011 02:07:15
 */
@MountHybrid
@MountPath(path = "secure/Schedule")
public class Schedule extends StaffPMWebPage {

    @SpringBean(name = "taskService")
    private TaskService taskService;
    @SpringBean(name = "userService")
    private UserService userService;
    private final CalendarPanel cal = new CalendarPanel("cal");
    private final Logger log = LoggerFactory.getLogger(getClass());

    public Schedule() {

        cal.setOutputMarkupId(true);
        cal.addCalendar(new Calendar(1, "StaffPM Tasks", 22));

        DateTime start = new DateTime().minusMonths(12).withDayOfMonth(1);
        DateTime end = new DateTime().plusMonths(12).withDayOfMonth(new DateTime().plusMonths(12).dayOfMonth().getMaximumValue());

        List<Event> events = taskService.getTasksDueBetweenForUser(userService.getCurrentUser(), start, end);
        if (userService.getCurrentUser().isGcalEnabled() && (userService.getCurrentUser().getGcalAuth() != null)) {
            events.addAll(GCalUtils.getAndProcessGcalEvents(userService, start, end));
            cal.addCalendar(new Calendar(2, "Google Calendar", 2));
        } else if (userService.getCurrentUser().isGcalEnabled() && (userService.getCurrentUser().getGcalAuth() == null)) {
            log.info("You have not autorised staffpm to access your google account.");
        }
        for (Event evt : events)
            cal.addEvent(evt);

        final AbstractDefaultAjaxBehavior behave = new AbstractDefaultAjaxBehavior() {

            private static final long serialVersionUID = 1L;

            @Override
            protected void respond(final AjaxRequestTarget target) {
                long viewStart = Long.parseLong(RequestCycle.get().getRequest().getParameter("viewStart"));
                long viewEnd = Long.parseLong(RequestCycle.get().getRequest().getParameter("viewEnd"));
                DateTime start = new DateTime(viewStart);
                DateTime end = new DateTime(viewEnd);
                getEvents(target, start, end);
            }
        };
        add(behave);

        add(cal);
    }

    protected void getEvents(AjaxRequestTarget target, DateTime start, DateTime end) {
        log.info("Got start: " + start.toString("dd MMM yy") + " end: " + end.toString("dd MMM yy"));
        List<Event> events = taskService.getTasksDueBetweenForUser(userService.getCurrentUser(), start.minusMonths(2), end.plusMonths(2));
        if (userService.getCurrentUser().isGcalEnabled() && (userService.getCurrentUser().getGcalAuth() != null)) {
            events.addAll(GCalUtils.getAndProcessGcalEvents(userService, start.minusMonths(2), end.plusMonths(2)));
        } else if (userService.getCurrentUser().isGcalEnabled() && (userService.getCurrentUser().getGcalAuth() == null)) {
            log.info("You have not autorised staffpm to access your google account.");
        }
        if (events.size() > 0)
            cal.clearEvents();
        for (Event evt : events)
            cal.addEvent(evt);
        cal.setView(start, end);
        target.addComponent(cal);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.StaffPMWebPage#getTitle()
     */
    @Override
    public String getTitle() {
        return "StaffPM - My Schedule";
    }

}
